import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o visible = false;
    @o isUpdate = false;
    @o modifyPassword = false;
    @o rolesList = [];//身份列表
    @o accountId = '';
    @o accountIdentity = '';
    @o accountPassword = '';
    @o accountName = '';
    @o accountPhone = '';


    will() {

    }

    closeDialog() {
        this.visible = false;
        this.isUpdate = false;
        this.modifyPassword = false;
        this.accountId = '';
        this.accountIdentity = '';
        this.accountPassword = '';
        this.accountName = '';
        this.accountPhone = '';
    }


    checkInput() {
        if (!this.accountIdentity || (!!this.accountIdentity && !this.accountIdentity.toString().trim())) {
            this.stores.Message.showMessage(3, '管理员身份不能为空');
            return true;
        }
        if (!this.accountName || (!!this.accountName && !this.accountName.toString().trim())) {
            this.stores.Message.showMessage(3, '管理员账号不能为空');
            return true;
        }
        if (!this.accountPhone || (!!this.accountPhone && !this.accountPhone.toString().trim())) {
            this.stores.Message.showMessage(3, '管理员手机号不能为空');
            return true;
        }
        // if ($$.utils.formRegular.phone(this.accountPhone)) {
        //     this.stores.Message.showMessage(3, '请输入正确的手机号格式');
        //     return true;
        // }
        if ((!this.isUpdate || this.modifyPassword) && !this.accountPassword || (!!this.accountPassword && !this.accountPassword.toString().trim())) {
            this.stores.Message.showMessage(3, '管理员密码不能为空');
            return true;
        }
    }

    confirmDialog() {
        if (this.checkInput()) {
            return;
        }
        this.stores.Frame.showLoading();
        let postData = this.isUpdate ?
            {
                phone: this.accountPhone,
                name: this.accountName,
                roleId: this.accountIdentity,
                userId: this.accountId,
            } :
            {
                roleId: this.accountIdentity,
                userName: this.accountName,
                phone: this.accountPhone,
                password: this.accountPassword,
            };
        postData = this.modifyPassword ? {
            userName: this.accountName,
            password: this.accountPassword,
        } : postData;
        let requestUrl = this.isUpdate ? $$.api('accountsUpdate') : $$.api('accountsInsert');
        requestUrl = this.modifyPassword ? $$.api('accountResetPwd') : requestUrl;
        request({
            url: requestUrl,
            data: postData,
            method: 'post'
        }).then(res => {
            this.stores.Message.showMessage(1, res.message);
            this.stores.Frame.hideLoading();
            this.fire('accountsDialog:update');
            this.closeDialog();
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });
    }


    getRolesList() {
        request({url: $$.api('getRoleList')}).then(res => {
            res.data && res.data.map(item => {
                // if (item.state === $$.SEM.ROLE_ACTIVE_STATE) {
                this.rolesList.push({value: item.id, label: item.name});
                // }
            });
        }).catch(err => {
            this.stores.Message.showMessage(2, err.message);
        });
    }

    passwordChange(v) {
        this.accountPassword = v;
    }

    nameChange(v) {
        this.accountName = v;
    }

    phoneChange(v) {
        this.accountPhone = v;
    }

    identityChange(v) {
        this.accountIdentity = v;
    }


    mount() {
        this.getRolesList();
        this.on('accountsDialog:show', (data) => {
            this.visible = true;
            if (data) {
                this.isUpdate = true;
                this.modifyPassword = data.modifyPassword || false;
                this.accountId = data.userId;
                this.accountIdentity = data.roleId;
                this.accountName = data.name;
                this.accountPhone = data.phone;
            }
        });
    }

}

export default Store;